package com.example.xianji.activity;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.example.xianji.Contant;
import com.example.xianji.XJApplication;
import com.example.xianji.R;
import com.example.xianji.adapter.HangYe_Adapter;
import com.example.xianji.bean.YiJi_Class;
import com.example.xianji.widgets.CustomProgressDialog;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Main_Choose_HangyeActivity extends Activity {
    public static ArrayList<Integer> indext = new ArrayList<Integer>();
    public static SparseBooleanArray array = new SparseBooleanArray();
    private ListView listview;
    private ArrayList<YiJi_Class> list = new ArrayList<YiJi_Class>();
    private HangYe_Adapter mHangYe_Adapter;
    private ImageView choose_hangye_back;
    private TextView text_queding;
    //private ArrayList<YiJi_Class> hangye_list;
    private CustomProgressDialog mCustomProgressDialog;
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                if (mCustomProgressDialog != null) {
                    mCustomProgressDialog.dismiss();
                    mCustomProgressDialog = null;
                }
                listview.setAdapter(mHangYe_Adapter);
                if (getIntent().getIntExtra("come_from", 0) == 2) {
                    for (int i = 0; i < list.size(); i++) {
                        for (int j = 0; j < Main_Bianji_QiYe_InformationActivity.yiji_list.size(); j++) {
                            if (Main_Bianji_QiYe_InformationActivity.yiji_list.get(j).getId() == list.get(i).getId()) {
                                array.put(i, true);
                                indext.add(list.get(i).getId());
                            }
                        }
                    }
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main__choose__hangye);
        //	XJApplication.getInstance().addActivity(this);
        choose_hangye_back = (ImageView) findViewById(R.id.choose_hangye_back);
        text_queding = (TextView) findViewById(R.id.text_queding);

        listview = (ListView) findViewById(R.id.listview);
        mHangYe_Adapter = new HangYe_Adapter(this, list, indext, array);

        /*hangye_list=Single.getInstance().getList();*/
        mCustomProgressDialog = CustomProgressDialog.createDialog(this);
        mCustomProgressDialog.setCanceledOnTouchOutside(false);
        mCustomProgressDialog.show();
        indext.clear();
        get_hangye_fenlei();

        choose_hangye_back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Main_Choose_HangyeActivity.this.finish();
            }
        });
        text_queding.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Main_Choose_HangyeActivity.this.finish();
            }
        });
    }

    public void get_hangye_fenlei() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        JSONObject params = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST, Contant.qiye_hangye_fenlei,
                params, new Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                try {
                    if (response.getInt("status") == 1) {
                        list.clear();
                        JSONArray array = response.getJSONArray("data");
                        for (int i = 0; i < array.length(); i++) {
                            JSONObject obj = array.getJSONObject(i);
                            YiJi_Class mYiJi_Class = new YiJi_Class();
                            mYiJi_Class.setId(obj.getInt("id"));
                            mYiJi_Class.setName(obj.getString("name"));
                            list.add(mYiJi_Class);
                        }
                    } else {
                        Toast.makeText(Main_Choose_HangyeActivity.this, response.getString("msg"), 0).show();
                    }

                    handler.sendEmptyMessage(1);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                if (mCustomProgressDialog != null) {
                    mCustomProgressDialog.dismiss();
                    mCustomProgressDialog = null;
                }
            }
        });
        XJApplication.getHttpQueues().cancelAll("abcPost");
        request.setTag("abcPost");
        request.setRetryPolicy(new DefaultRetryPolicy(50 * 1000, 1, 1.0f));
        XJApplication.getHttpQueues().add(request);
        XJApplication.getHttpQueues().start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }
}
